﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using FuelDXApplication1.Entity;
using DevExpress.DataAccess.Native.Data;
using DevExpress.CodeParser;


namespace FuelDXApplication1.TmzWinform
{
    public partial class AlreadyModified : Form
    {

        private int pageIndex = 1;
        private int pageSize = 2;
        int pageCount;
        int totalCount;
        public AlreadyModified()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 已修改待上报页面显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void AlreadyModified_Load(object sender, EventArgs e)
        {
            GetBasicInfos();
            //绑定燃料类型
            GetFuelType();
            //绑定车辆类型
            GetVehicleTypeInFo();
            BindLookUpEdit3();
        }
        public class NameValue
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }
        /// <summary>
        /// 绑定每页显示条数下拉框
        /// </summary>
        private void BindLookUpEdit3()
        {
            List<NameValue> list = new List<NameValue>(){
                  new NameValue() { Name="2条/页",Value="2"},
                  new NameValue() { Name="3条/页",Value="3"},
                  new NameValue() { Name="4条/页",Value="4"},
                  new NameValue() { Name="5条/页",Value="5"}
          };
            //包含两个字段，Name为显示的字段，value为实际值字段。
            //插入了一个binding,将lookupedit的editvalue绑定实体的Value字段。
            lookUpEdit3.Properties.DataSource = list;
            lookUpEdit3.Properties.DisplayMember = "Name";  //输入框中要显示的值         
            lookUpEdit3.Properties.ValueMember = "Value";
            lookUpEdit3.DataBindings.Add(new Binding("EditValue", list, "Value"));
        }
        public record PageGetBase(int pageIndex, int pageSize);
        /// <summary>
        /// 获取基本信息
        /// </summary>
        private void GetBasicInfos()
        {
            PageGetBase page = new PageGetBase(pageIndex, pageSize);
            // 使用 DbHelper 获取数据
            var dataTable = DBHelper.Query<BasicInformation>();
            // gridControl1.DataSource = dataTable.Result;
            totalCount = dataTable.Result.Count();
            pageCount = (int)Math.Ceiling(totalCount * 1.0 / page.pageSize);
            var pageInfo = dataTable.Result.OrderBy(x => x.FuelParameter).Skip((page.pageIndex - 1) * page.pageSize).Take(page.pageSize);
            gridControl1.DataSource = pageInfo;
            labelControl15.Text = pageSize.ToString();//每页显示条数
            labelControl14.Text = pageIndex.ToString();//当前页
            labelControl16.Text = pageCount.ToString();//总页数
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            string iCPRecordNumber = textEdit1.Text;
            string vehicleModel = textEdit2.Text;
            var selecttext = lookUpEdit1.EditValue;
            var versionId = lookUpEdit2.EditValue;
            var sdate = dateEdit1.EditValue;
            var edate = dateEdit2.EditValue;

            var list = DBHelper.Query<BasicInformation>();
            if (list.Result != null)
            {
                // 使用多个条件进行筛选
                var filterdata = list.Result.AsQueryable();

                // 根据ICPRecordNumber（iCPRecordNumber）筛选
                if (!string.IsNullOrEmpty(iCPRecordNumber))
                {
                    filterdata = filterdata.Where(b => b.ICPRecordNumber == iCPRecordNumber);
                }

                // 根据VehicleModel（vehicleModel）筛选
                if (!string.IsNullOrEmpty(vehicleModel))
                {
                    filterdata = filterdata.Where(b => b.VehicleModel == vehicleModel);
                }
                if (selecttext != null)
                {
                    filterdata = filterdata.Where(b => b.FuelTypeId == Convert.ToInt32(selecttext));
                }
                if (versionId != null)
                {
                    filterdata = filterdata.Where(b => b.VersionId == Convert.ToInt32(versionId));
                }
                if (sdate != null)
                {
                    filterdata = filterdata.Where(b => b.DateOfImport >= Convert.ToDateTime(sdate));
                }
                if (edate != null)
                {
                    filterdata = filterdata.Where(b => b.DateOfImport <= Convert.ToDateTime(edate));
                }
                gridControl1.DataSource = filterdata.ToList();
            }
            else
            {
                // 如果没有数据，清空 gridControl
                gridControl1.DataSource = null;
            }
        }
        /// <summary>
        /// 绑定燃料类型下拉
        /// </summary>
        public async void GetFuelType()
        {
            var fuelType = await DBHelper.Query<FuelType>();
            this.lookUpEdit1.Properties.DataSource = fuelType;
            this.lookUpEdit1.Properties.ValueMember = "FuelTypeId";
            this.lookUpEdit1.Properties.DisplayMember = "FuelTypeName";
        }
        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        public async void GetVehicleTypeInFo()
        {
            var vehicleTypeInFo = await DBHelper.Query<VehicleTypeInFo>();
            this.lookUpEdit2.Properties.DataSource = vehicleTypeInFo;
            this.lookUpEdit2.Properties.ValueMember = "VehicleTypeId";
            this.lookUpEdit2.Properties.DisplayMember = "VehicleTypeName";
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEdit1_Click(object sender, EventArgs e)
        {
            if (pageIndex != 1)
            {
                pageIndex = 1;
                GetBasicInfos();
            }
            else
            {
                MessageBox.Show("已经是首页了！");
            }
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEdit4_Click(object sender, EventArgs e)
        {
            if (pageIndex != pageCount)
            {
                pageIndex = pageCount;
                GetBasicInfos();
            }
            else
            {
                MessageBox.Show("已经是尾页了！");
            }
           
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEdit3_Click(object sender, EventArgs e)
        {
            if (pageIndex != 0)
            {
                pageIndex--;
                GetBasicInfos();
            }
            else
            {
                MessageBox.Show("已经是第一页了！");
            }
        }
         
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEdit2_Click(object sender, EventArgs e)
        {
            if (pageIndex != pageCount)
            {
                pageIndex++;
                GetBasicInfos();
            }
            else
            {
                MessageBox.Show("已经是最后一页了！");
            }
         
        }
        /// <summary>
        /// 每页显示条数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lookUpEdit3_EditValueChanged_1(object sender, EventArgs e)
        {
            if (lookUpEdit3.EditValue != null)
            {
                pageSize = int.Parse(lookUpEdit3.EditValue.ToString());
                GetBasicInfos();
            }
        }
        /// <summary>
        /// 指定页跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textEdit3_KeyPress(object sender, KeyPressEventArgs e)
        {
            //在KeyPress事件处理程序中，判断按下的键是否是回车键
            if (e.KeyChar == 13) // 回车键的ASCII码是13
            {   
                 string text = textEdit3.Text;
                 if (!System.Text.RegularExpressions.Regex.IsMatch(text, @"^\d+$"))
                 {
                     MessageBox.Show("请输入数字！");
                     textEdit3.Focus();
                 }
                else
                {
                    if(int.Parse(textEdit3.Text)>pageCount)
                    {
                        MessageBox.Show("请输入有效页！");
                    }
                    else
                    { 
                        pageIndex = int.Parse(textEdit3.Text);
                        GetBasicInfos();
                    }
                  
                }
            }
            //else
            //{
            //    MessageBox.Show("请输入回车键跳转");

            //}
        }

      
    }
}
